package com.ruoyi.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.domain.AdvInfoliks;
import com.ruoyi.mapper.AdvDevicerelationMapper;
import com.ruoyi.mapper.AdvInfoliksMapper;
import com.ruoyi.mapper.AdvProductsMapper;
import com.ruoyi.utils.MyFileUtils;
import com.ruoyi.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.AdvPlatinfosMapper;
import com.ruoyi.domain.AdvPlatinfos;
import com.ruoyi.service.IAdvPlatinfosService;

import javax.annotation.Resource;

/**
 *  Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class AdvPlatinfosServiceImpl implements IAdvPlatinfosService
{
    @Autowired
    @Resource
    private AdvPlatinfosMapper advPlatinfosMapper;
    @Autowired
    @Resource
    private AdvDevicerelationMapper advDevicerelationMapper;
    @Autowired
    @Resource
    private AdvInfoliksMapper advInfoliksMapper;


    /**
     * 查询
     *
     * @param advPlatinfosId  主键
     * @return
     */
    @Override
    public AdvPlatinfos selectAdvPlatinfosByAdvPlatinfosId(Long advPlatinfosId)
    {
        return advPlatinfosMapper.selectAdvPlatinfosByAdvPlatinfosId(advPlatinfosId);
    }

    /**
     * 查询 列表
     *
     * @param advPlatinfos
     * @return
     */
    @Override
    public List<AdvPlatinfos> selectAdvPlatinfosList(AdvPlatinfos advPlatinfos)
    {
        List<AdvPlatinfos> list = advPlatinfosMapper.selectAdvPlatinfosList(advPlatinfos);
        System.out.println("获取到的数据: " + list);
        return list;
    }

    /**
     * 新增
     *
     * @param advPlatinfos
     * @return 结果
     */
    @Override
    public int insertAdvPlatinfos(AdvPlatinfos advPlatinfos)
    {
        advPlatinfos.setAdvPlatinfosId(UUIDUtils.timeMillisId());
        advPlatinfos.setTopFlag(2L);
        advPlatinfos.setPubDatetime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .format(new Date()));

        if(!Objects.isNull(advPlatinfos.getLrcurl())) {
            if (advPlatinfos.getLrcurl().equals("") || advPlatinfos.getLrcurl().isEmpty()){
                advPlatinfos.setMediafileSize("0");
                System.out.println("修改后的值 =====> " + advPlatinfos);
            }
        } else {
            advPlatinfos.setMediafileSize("0");
            System.out.println("修改后的值 =====> " + advPlatinfos);
        }
        return advPlatinfosMapper.insertAdvPlatinfos(advPlatinfos);
    }

    /**
     * 修改
     *
     * @param advPlatinfos
     * @return 结果
     */
    @Override
    public int updateAdvPlatinfos(AdvPlatinfos advPlatinfos)
    {
        System.out.println("提交的数据 =====> " + advPlatinfos);
        System.out.println("提交的url ======>" + advPlatinfos.getLrcurl() + "<==========");
        if(!Objects.isNull(advPlatinfos.getLrcurl())) {
            if (advPlatinfos.getLrcurl().equals("") || advPlatinfos.getLrcurl().isEmpty()){
                advPlatinfos.setMediafileSize("0");
                System.out.println("修改后的值 =====> " + advPlatinfos);
            }
        } else {
            advPlatinfos.setMediafileSize("0");
            System.out.println("修改后的值 =====> " + advPlatinfos);
        }
        advPlatinfosMapper.deleteByAdvPlatinfos(advPlatinfos.getAdvPlatinfosId());
        return advPlatinfosMapper.updateAdvPlatinfos(advPlatinfos);
    }

    /**
     * 批量删除
     *
     * @param advPlatinfosIds 需要删除的 主键
     * @return 结果
     */
    @Override
    public int deleteAdvPlatinfosByAdvPlatinfosIds(Long[] advPlatinfosIds)
    {
        List<AdvPlatinfos> advPlatinfosList = new ArrayList<AdvPlatinfos>();
        for (Long advPlatinfosId : advPlatinfosIds) {
            // 查询到数组中id所有的资讯
            AdvPlatinfos platinfos = advPlatinfosMapper.selectAdvPlatinfosByAdvPlatinfosId(advPlatinfosId);
            if(!Objects.isNull(platinfos)) {
                advPlatinfosList.add(platinfos);
            }
            // 还需要删除点赞以及 设备与视频内容关联表中的数据
            advInfoliksMapper.deleteAdvInfoliksByAdvPlatinfosId(advPlatinfosId);
            advDevicerelationMapper.deleteAdvDevicerelationByAdvPlatinfosId(advPlatinfosId);
        }
        int i = advPlatinfosMapper.deleteAdvPlatinfosByAdvPlatinfosIds(advPlatinfosIds);
        if(i > 0) {
            // 如果长度大于一, 就要删除对应的图片
            for (AdvPlatinfos platinfos : advPlatinfosList) {
                MyFileUtils.deleteFile(platinfos.getVideourl());
                MyFileUtils.deleteFile(platinfos.getPhotoimage());
            }
        }
        return i;
    }

    /**
     * 删除 信息
     *
     * @param advPlatinfosId  主键
     * @return 结果
     */
    @Override
    public int deleteAdvPlatinfosByAdvPlatinfosId(Long advPlatinfosId)
    {
        try {
            AdvPlatinfos platinfos = advPlatinfosMapper.selectAdvPlatinfosByAdvPlatinfosId(advPlatinfosId);
            int i = advPlatinfosMapper.deleteAdvPlatinfosByAdvPlatinfosId(advPlatinfosId);
            MyFileUtils.deleteFile(platinfos.getPhotoimage());
            MyFileUtils.deleteFile(platinfos.getVideourl());
            return i;
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("请先删除关联数据");
        }
    }

    /**
     * 限制只有一个默认音频
     */
    @Override
    public void exeUpdateDefault() {
        advPlatinfosMapper.exeUpdateDefault();
    }

    @Override
    public int exeUpdateByDefault(Long advPlatinfosId) {
        advPlatinfosMapper.exeUpdateDefault();
        return advPlatinfosMapper.exeUpdateDefaultById(advPlatinfosId);
    }

    @Override
    public List<AdvPlatinfos> selectAdvPlatinfosListVoice(String advMembersId, String devicesno) {
        List<AdvPlatinfos> list = advPlatinfosMapper.selectAdvPlatinfosListVoice(advMembersId, devicesno);
        return list;
    }

}
